﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace FTchina.Caching
{
    /// <summary>
    ///FTchinaCache 的摘要说明
    /// </summary>
    public class FTchinaCache
    {
        public static readonly int seconds = 2;//默认缓存保留2秒钟
        /// <summary>
        /// 添加一项Cache
        /// </summary>
        /// <param name="key">Cache标志</param>
        /// <param name="value">Cache具体内容</param>
        /// <returns></returns>
        public static Boolean AddCache(string key, string value)
        {
           
            HttpRuntime.Cache.Insert(key, value, null, DateTime.UtcNow.AddSeconds(seconds), TimeSpan.Zero);
            return true;
        }
        /// <summary>
        /// 获取一个Cache
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>缓存的内容</returns>
        public static string GetCache(string key)
        {
            return HttpRuntime.Cache.Get(key).ToString();
        }



        /// <summary>
        /// 判断指定的缓存是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool IsExits(string key)
        {
           
            
           return  HttpRuntime.Cache.Get(key) != null ;
              
        }
        public static void RemoveAllCache()
        {
            System.Collections.IDictionaryEnumerator ss = HttpRuntime.Cache.GetEnumerator();


            while (ss.MoveNext())
            {

                try
                {
                    HttpRuntime.Cache.Remove(ss.Key.ToString());
                }
                catch { }
            }

        }
    }
}